package com.larissa.android.quiz.fragments;

import android.annotation.SuppressLint;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.larissa.android.quiz.question.Question;
import com.larissa.android.quiz.QuestionActivity;
import com.larissa.android.quiz.question.QuestionRepository;
import com.larissa.android.quiz.R;
import com.larissa.android.quiz.WelcomeActivity;

import java.util.List;

public class QuestionFragment extends BasicFragment {
    private List<Question> questions;
    private RecyclerView recyclerView;
    private QuestionAdapter adapter;
    private LinearLayoutManager manager;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_question;
    }

    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        recyclerView = rootView.findViewById(R.id.fragment_recyclerView);
        manager = new LinearLayoutManager(rootView.getContext());
        if (getActivity() instanceof QuestionActivity) {
            QuestionActivity activity = (QuestionActivity) getActivity();
            if(activity.getCurrentCategory() == WelcomeActivity.GEOGRAPHY)
            {
                questions = QuestionRepository.getInstance().getGeoQuestions();
            }else
            {
                questions = QuestionRepository.getInstance().getMathQuestions();
            }
            if(activity.getCurrentScreen() == RecyclerView.HORIZONTAL)
            {
                manager.setOrientation(RecyclerView.HORIZONTAL);
                adapter = new QuestionAdapter(questions, this, true);
            } else
            {
                manager.setOrientation(RecyclerView.VERTICAL);
                adapter = new QuestionAdapter(questions, this, false);
            }
            recyclerView.setLayoutManager(manager);
            recyclerView.setAdapter(adapter);
        }
    }

    public void setCategoryAndScreen(int targetCategory, int targetScreen) {
        if (isCategoryToBeUpdated(targetCategory)) {
            if (targetCategory == WelcomeActivity.GEOGRAPHY) {
                questions = QuestionRepository.getInstance().getGeoQuestions();
            } else {
                questions = QuestionRepository.getInstance().getMathQuestions();
            }
            adapter.setQuestions(questions);
        }
        else if (isScreenToBeUpdated(targetScreen)) {
            if (targetScreen == RecyclerView.HORIZONTAL) {
                manager.setOrientation(RecyclerView.HORIZONTAL);
                adapter = new QuestionAdapter(questions, this, true);
            } else {
                manager.setOrientation(RecyclerView.VERTICAL);
                adapter = new QuestionAdapter(questions, this, false);
            }
            recyclerView.setAdapter(adapter);
        }
    }

    private boolean isCategoryToBeUpdated(int targetCategory) {
        if (targetCategory == WelcomeActivity.GEOGRAPHY) {
            return questions != QuestionRepository.getInstance().getGeoQuestions();
        } else {
            return questions != QuestionRepository.getInstance().getMathQuestions();
        }
    }

    private boolean isScreenToBeUpdated(int targetScreen) {
        return targetScreen != manager.getOrientation();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void notifyRecyclerViewAdapter()
    {
        adapter.notifyDataSetChanged();
    }

}
